#include "cooselevescene.h"
#include <QMenuBar>
#include <QPainter>
#include <QPixmap>
#include "mypushbutton.h"
#include <QPushButton>
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include <QString>
#include <QSound>

CooseLeveScene::CooseLeveScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(320,588);

    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    //设置标题
    this->setWindowTitle("请彪少选择关卡场景");

    //创建菜单栏
    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);

    //我知道你想说什么，仔细看下面两个函数的返回类型，返回的是指针类型
    //就是我通过调用主窗口的成员函数，menuBar(),生成了主窗口的菜单栏，
    //返回值是一个指向我菜单栏的指针，我可以通过这个指针对我的菜单进行操作
    //比如在菜单栏中加个菜单，调用菜单栏的addMenu(),生成了一个菜单，
    //返回指向这个菜单的指针。
    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");

    //创建菜单项
    QAction * quitAction = startMenu->addAction("退出");
    //音效
    QSound * chooseSound = new QSound(":/res/TapButtonSound.wav",this);
    QSound * backSound = new QSound(":/res/BackButtonSound.wav",this);

    connect(quitAction,&QAction::triggered,this,[=](){
        this->close();

    });

    //返回按钮
    //按钮不像pixmap，不用设置宽高
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    //按钮一定要设置父亲是谁，属于谁的按钮
    backBtn->setParent(this);
    backBtn->move( (this->width() - backBtn->width()) / 2, this->height() - backBtn->height() - 30 );

    //点击返回
    //第二个参数是函数指针
    connect(backBtn, &MyPushButton::clicked, this, [=](){
        qDebug() << "点击了返回按钮";
        backSound->play();
        //告诉主场景，我返回了，主场景监听ChooseLevelScence的返回按钮
        QTimer::singleShot(300,this,[=](){
            emit this->chooseSceneBack();
        });
    });

    //创建选择关卡的按钮
    for(int i = 0; i < 20; i++)
    {
        MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move( (this->width()/2 - (menuBtn->width()*2+15)) + i%4*(menuBtn->width()+10) , 150 + i/4*(menuBtn->height()+15) );

        connect(menuBtn, &MyPushButton::clicked, [=](){
            QString str = QString("您选择的是第 %1 关").arg(i + 1);
            qDebug() << str;

            chooseSound->play();
            this->hide();


            //进入到游戏场景
            play = new PlayScene(i + 1);

            //设置游戏场景的位置
            play->setGeometry(this->geometry());
            //显示
            play->show();

            connect(play, &PlayScene::chooseSceneBack, this, [=](){
                this->setGeometry(play->geometry());
                this->show();
                delete play;
                play = nullptr;
            });

        });
//        menuBtn->setText( QString::number(1+i));

        QLabel * label = new QLabel;

        QFont font;
        font.setFamily("青鸟华光简行草");
        font.setPointSize(20);
        font.setWeight(QFont::Black);
        label->setFont(font);

        label->setParent(this);
        label->setFixedSize(menuBtn->width(), menuBtn->height());
        label->setText(QString::number(i+1));
        label->move( (this->width()/2 - (menuBtn->width()*2+15)) + i%4*(menuBtn->width()+10) , 145 + i/4*(menuBtn->height()+15) );
        label->setAlignment( Qt::AlignHCenter|Qt::AlignVCenter );
        //设置让鼠标设置穿透 51 号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }
}

void CooseLeveScene::paintEvent(QPaintEvent *event)
{
    //绘画事件中的画家和pixmap都是栈对象
    QPainter painter(this);
    QPixmap pixmap(":/res/OtherSceneBg.png");
    //pixmap.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pixmap);

    pixmap.load(":/res/Title.png");
    painter.drawPixmap( (this->width() - pixmap.width())*0.5, 30, pixmap.width(), pixmap.height(), pixmap);;
}
